package com.hangwei.paperless.fragment.sub;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.hangwei.paperless.EditSubSetListActivity;
import com.hangwei.paperless.R;
import com.hangwei.paperless.SubRepairActivity;
import com.hangwei.paperless.adapter.HorizontalPicAdapter;
import com.hangwei.paperless.adapter.sub.SubSetAdapter;
import com.hangwei.paperless.model.bean.DataFilling;
import com.hangwei.paperless.model.bean.SubSetInfo;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.twelve.all.base.LoadFragment;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张龙臣 on 2020/11/27.
 * 衬套装配单
 */

public class SubSetListFragment extends LoadFragment {
    private String mBillId;
    private SubRepairActivity mActivity;
    private HorizontalPicAdapter mPicAdapter;
    private SubSetAdapter mAdapter;
    private String mSubId;
    private DataFilling mDataFilling;
    private String mUserId;

    @Override
    protected View onRealCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mBillId = arguments.getString("billId");
            mSubId = arguments.getString("id");
            mDataFilling = (DataFilling) arguments.getSerializable("data");
            mActivity = (SubRepairActivity) getActivity();
            mUserId = SPUtil.getString(SPKey.Account.USER_ID);
        }
        View inflate = inflater.inflate(R.layout.footer_sub_set_list, container, false);
        TextView tvDes = inflate.findViewById(R.id.tvDes);
        LinearLayout llDes = inflate.findViewById(R.id.llDes);
        llDes.setVisibility(TextUtils.isEmpty(mDataFilling.remark)?View.GONE:View.VISIBLE);
        tvDes.setText(Tools.checkedString(mDataFilling.remark));
        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new SubSetAdapter(null);
        mAdapter.addFooterView(getFooter());
        recyclerView.setAdapter(mAdapter);
        recyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.llItemView) {
                    if (mDataFilling.isNA) return;
                    SubSetInfo info = mAdapter.getData().get(position);
                    Intent intent = new Intent(mActivity, EditSubSetListActivity.class);
                    intent.putExtra("itemId", info.id);
                    intent.putExtra("billId", mBillId);
                    intent.putExtra("id", mSubId);
                    intent.putExtra("picList", mDataFilling == null ? null : mDataFilling.picList);
                    startActivity(intent);
                }
            }
        });
        initData();
        return inflate;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
        loadingDialog.show();
        StaticPresenter.getBushInstallationSheet(mUserId, mBillId, mSubId, new CallBack<List<SubSetInfo>>() {
            @Override
            public void onCompleted(List<SubSetInfo> subSetInfos) {
                loadingDialog.dismiss();
                mAdapter.setNewData(subSetInfos);
            }

            @Override
            public void onError() {
                loadingDialog.dismiss();
            }
        });
    }

    private View getFooter() {
        View inflate = LayoutInflater.from(mActivity).inflate(R.layout.footer_sub_part_check, null);
        RecyclerView recyclerViewPic = inflate.findViewById(R.id.recyclerViewPic);
        LinearLayoutManager layout = new LinearLayoutManager(mActivity);
        layout.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewPic.setLayoutManager(layout);
        mPicAdapter = new HorizontalPicAdapter(mDataFilling == null ? null : mDataFilling.picList);
        recyclerViewPic.setAdapter(mPicAdapter);
        recyclerViewPic.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.sdv)
                    mActivity.showBigPic((ArrayList<String>) mPicAdapter.getData(), position);
            }
        });
        return inflate;
    }
}
